package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bean.Admin;
import com.bean.Homework;
import com.bean.Student;
import com.bean.Teacher;

public class AdminDao extends DaoUtil{ 
	private PreparedStatement stat = null;
	private Connection conn = null;
	private ResultSet rs = null;
	
	//登录查询
	public Admin findAdm(Admin adm){
		conn = this.getConnection();
		String sql = "select * from user where id = ? and password = ?";
		try {
			stat = conn.prepareStatement(sql);
			stat.setInt(1,adm.getAdmUserID());
			stat.setString(2, adm.getAdmPassWord().trim());
			rs = stat.executeQuery();
			if(rs.next()){
				return adm;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally{
			closed();
		}
	
		return null;
	}
	
	//查找所有学生信息
	public List<Student> findStu(){
		List<Student> studentList = new ArrayList<Student>();
		conn = this.getConnection();
		
		//List list = new ArrayList();
		String sql = "select * from stuuserinfo";
		try {
			stat = conn.prepareStatement(sql);
			rs = stat.executeQuery();
			while (rs.next()) {
				Student stu = new Student();
				stu.setStuUserID(rs.getInt(1));
				//stu.setStuPassword(rs.getString(2));
				stu.setStuName(rs.getString(2));
				stu.setBelongclass(rs.getString(3));
				stu.setStuEmail(rs.getString(4));
				stu.setMajor(rs.getString(5));
				studentList.add(stu);
				
			}
			System.out.println("管理员查询所有学生信息成功");
			return studentList;	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
			
		} finally {
			closed();
		}
		return studentList;		
		
	}
	//查找特定学生信息
		public List<Student> findSpecialStu(int stuID){
		List<Student> studentList = new ArrayList<Student>();
		conn = this.getConnection();
		
		//List list = new ArrayList();
		String sql = "select * from stuuserinfo where stuUserID like "+"'%"+stuID+"%'";
		try {
			stat = conn.prepareStatement(sql);
			//stat.setInt(1, stuID);
			rs = stat.executeQuery();
			while (rs.next()) {
				Student stu = new Student();
				stu.setStuUserID(rs.getInt(1));
				//stu.setStuPassword(rs.getString(2));
				stu.setStuName(rs.getString(2));
				stu.setBelongclass(rs.getString(3));
				stu.setStuEmail(rs.getString(4));
				stu.setMajor(rs.getString(5));
				studentList.add(stu);
				
			}
			System.out.println("管理员查询特定学生信息成功");
			return studentList;	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
			
		} finally {
			closed();
		}
		return studentList;		
		
	}
		//查询精确地学生信息
		public List<Student> findRefinedStu(Student s){
			List<Student> studentList = new ArrayList<Student>();
			conn = this.getConnection();
			String sql = "select * from stuuserinfo where stuUserID=? and stuName=? and belongclass=? and major=?";
			try {
				stat = conn.prepareStatement(sql);
				stat.setInt(1, s.getStuUserID());
				stat.setString(2, s.getStuName());
				stat.setString(3, s.getBelongclass());
				stat.setString(4, s.getMajor());
				rs = stat.executeQuery();
				while (rs.next()) {
					Student stu = new Student();
					stu.setStuUserID(rs.getInt(1));
					stu.setStuName(rs.getString(2));
					stu.setBelongclass(rs.getString(3));
					stu.setStuEmail(rs.getString(4));
					stu.setMajor(rs.getString(5));
					studentList.add(stu);
					
				}
				System.out.println("管理员查询特定学生信息成功");
				return studentList;	
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				
				e.printStackTrace();
				
			} finally {
				closed();
			}
			return studentList;		
			
		}
		
		
		//查询特定教师信息
		public List<Teacher> findSpecialTea(int teaID){
			List<Teacher> teacherList = new ArrayList<Teacher>();
			conn = this.getConnection();
			
			//List list = new ArrayList();
			String sql = "select * from teauserinfo where teaUserID like "+"'%"+teaID+"%'";
			try {
				stat = conn.prepareStatement(sql);
				rs = stat.executeQuery();
				while (rs.next()) {
					Teacher tea = new Teacher();
					tea.setTeaUserID(rs.getInt(1));
					tea.setTeaName(rs.getString(2));
					tea.setTeaEmail(rs.getString(3));
					
					teacherList.add(tea);
					
				}
				System.out.println("管理员查询所有教师信息成功");
				return teacherList;	
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				
				e.printStackTrace();
				
			} finally {
				closed();
			}
			return teacherList;		
			
		}
	//查找所有教师信息
	public List<Teacher> findTea(){
		List<Teacher> teacherList = new ArrayList<Teacher>();
		conn = this.getConnection();
		
		//List list = new ArrayList();
		String sql = "select * from teauserinfo";
		try {
			stat = conn.prepareStatement(sql);
			rs = stat.executeQuery();
			while (rs.next()) {
				Teacher tea = new Teacher();
				tea.setTeaUserID(rs.getInt(1));
				tea.setTeaName(rs.getString(2));
				tea.setTeaEmail(rs.getString(3));
				
				teacherList.add(tea);
				
			}
			System.out.println("管理员查询所有教师信息成功");
			return teacherList;	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
			
		} finally {
			closed();
		}
		return teacherList;		
		
	}
	
	
	public boolean deletetea(int id) {
		boolean flag = false;
		conn = this.getConnection();
		String sql1 = "delete from tealogin where teaUserID=?";
		String sql2 = "delete from teauserinfo where teaUserID=?";
		try {
			stat = conn.prepareStatement(sql1);
			stat.setInt(1, id);
			int i = stat.executeUpdate();
			stat = conn.prepareStatement(sql2);
			stat.setInt(1, id);
			int ii = stat.executeUpdate();
			System.out.println(i);
			System.out.println(ii);

			if(i==1&&ii==1){
				return flag=true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			
		} finally{
			closed();
		}
	
		return flag;
	}
	
	
	public boolean deletestu(int id) {
		boolean flag = false;
		conn = this.getConnection();
		String sql1 = "delete from stulogin where stuUserID=?";
		String sql2 = "delete from stuuserinfo where stuUserID=?";
		try {
			stat = conn.prepareStatement(sql1);
			stat.setInt(1, id);
			int i = stat.executeUpdate();
			stat = conn.prepareStatement(sql2);
			stat.setInt(1, id);
			int ii = stat.executeUpdate();
			System.out.println(i);
			System.out.println(ii);

			//if(i > 0&&ii > 0){
				return flag=true;
		//	}
		} catch (SQLException e) {
			e.printStackTrace();
			return flag;
		} finally{
			closed();
		}
	
		
	}
	
	public int updateP(Admin adm ,String newP){
		conn = this.getConnection();
		String sql = "UPDATE user SET password = ? WHERE id = ?";
		try {
			stat = conn.prepareStatement(sql);
			stat.setString(1,newP);
			stat.setInt(2, adm.getAdmUserID());
			return stat.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
			return 0;
		}finally {
			closed();
		}
		
	}
	/*public Student findStuInfo(int stuUserID){
		conn = this.getConnection();
		Student stu = new Student();
		//List list = new ArrayList();
		String sql = "select * from stuuserinfo where stuUserID = ?";
		try {
			stat = conn.prepareStatement(sql);
			stat.setInt(1, stuUserID);
			rs = stat.executeQuery();
			while (rs.next()) {				
				stu.setStuUserID(rs.getInt(1));
				stu.setStuName(rs.getString(2));
				stu.setBelongclass(rs.getString(3));
				stu.setStuEmail(rs.getString(4));
				stu.setMajor(rs.getString(5));
				System.out.println(rs.getInt(1));
				System.out.println(rs.getString(2));
				System.out.println("dfdfdf");
			}
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
			
		} finally {
			closed();
		}
		return stu;		
	}*/


	
}
